//
//  RootTabBarVC.swift
//  UNHimalayas
//
//  Created by MaHaoZhe on 2019/11/19.
//  Copyright © 2019 HachiTech. All rights reserved.
//

import UIKit

class RootTabBarVC: UITabBarController,UITabBarControllerDelegate
{

    override func viewDidLoad()
    {
        super.viewDidLoad()

        self.tabBar.tintColor = UIColor.white
        
        self.setupSubVC()
        
        self.delegate = self
        
        //方式tabbar上边的图标和文字有偏移动画
        UITabBar.appearance().isTranslucent = false
    }
    

    func setupSubVC()
    {
        let titleArr = ["首页","我听","1234","发现","账号"]
        let normalArr = ["host_home_page_tab_012","host_my_listen_tab_012","1234","host_find_tab_012","host_mine_tab_012"]
        let selectedArr = ["host_home_page_tab_152","host_my_listen_tab_152","1234","host_find_tab_152","host_mine_tab_152"]
        
        
        let showVCArr = [UNHomeVC(),UNListenVC(),UNPlayVC(),UNFindVC(),UNAccountVC()]
        let navigationArr = NSMutableArray()
        
        for (index, controller) in showVCArr.enumerated()
        {
            controller.tabBarItem.title = titleArr[index]
            controller.tabBarItem.image = UIImage.init(named: normalArr[index])?.withRenderingMode(.alwaysOriginal)
            controller.tabBarItem.selectedImage = UIImage.init(named: selectedArr[index])?.withRenderingMode(.alwaysOriginal)
            controller.tabBarItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.lightGray], for: UIControl.State())
            controller.tabBarItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.black], for: .selected)
            controller.tabBarItem.setTitleTextAttributes([
                NSAttributedString.Key.foregroundColor: UIColor.init(red: 229/255.0, green: 109/255.0, blue: 77/255.0, alpha: 1.0),
                NSAttributedString.Key.font: UIFont(name: "Heiti SC", size: 16.0)!
            ], for: .selected)
            
            let addNavVC = RootNavigationVC.init(rootViewController: controller)
            
            controller.navigationItem.title = titleArr[index]
            
            navigationArr.add(addNavVC)
        }
        self.viewControllers = navigationArr.mutableCopy() as! [RootNavigationVC]
        
        let centerBtn = UIButton.init(frame: CGRect.init(x: 0, y: 0, width: 44, height: 44))
        
        centerBtn.addTarget(self, action: #selector(centerBtnClickAction), for: .touchUpInside)
        self.tabBar.addSubview(centerBtn)
        centerBtn.centerX = self.tabBar.centerX
        let img = UIImageView.init(image: UIImage.init(named: "tabbar_np_playnon"))
        img.frame = CGRect.init(x: 0, y: 5, width: centerBtn.width, height: centerBtn.height)
        centerBtn.addSubview(img)
        
    }
    
    func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController)
    {
        if (viewController == (tabBarController.viewControllers![2]))
        {
            return ;
        }
    }
    
    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool
    {
        
        if (viewController == (tabBarController.viewControllers![2]) as UIViewController)
        {
            return false
        }
        
        return true
    }
    
//MARK: - 交互事件
    @objc func centerBtnClickAction()
    {
        
    }
    
}
